Skip to main content

JS 获取今日 昨天 上月开始结束时间

获取上月开始时间 和 结束时间

var nowdays = new Date(); 
var year = nowdays.getFullYear();
var month = nowdays.getMonth();
if(month==0){
month = 12;
year = year-1;
}
if(month<10){
month = '0'+month;
}
var myDate = new Date(year,month,0);
var startDate = year+'-'+month+'-01'; //上个月第一天
var endDate = year+'-'+month+'-'+myDate.getDate();//上个月最后一天

alert(startDate);alert(endDate);

获取今日

//昨天的时间
var day1 = new Date();
day1.setTime(day1.getTime()-24*60*60*1000);
var yesterday = day1.getFullYear()+"-" + (day1.getMonth()+1) + "-" + day1.getDate();
//今日日期
var day2 = new Date();
day2.setTime(day2.getTime());
var today = day2.getFullYear()+"-" + (day2.getMonth()+1) + "-" + day2.getDate();
//去年今日的时间
var lastyear = (day2.getFullYear() - 1) +"-" + (day2.getMonth()+1) + "-" + day2.getDate();